<?php

require_once 'general/classes/class.User.php';

class SessionController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		if(isset($_GET['e'])){
			switch($_GET['e']){
				case 1:
					$this->view->message = 'Debe ingresar todos los datos';
					break;
				case 2:
					$this->view->message = 'Los datos ingresados no son v&aacute;lidos';
					break;
				case 3:
					$this->view->message = 'El usuario fue bloqueado';
					break;
				case 4:
					$this->view->message = 'Su usuario a&uacute;n no ha sido activado.';
					break;
				default:

			}
		}

		$this->view->page_title = 'Ingrese sus datos para ingresar a la penca.';
		$this->_helper->layout()->setLayout('login');
			
		// action body
	}

	public function loginAction()
	{
		$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

		if($_POST['email'] && $_POST['password'] && $_POST['penca']){

			$user = new User();

			$auth = $user->authenticate($_POST['email'], $_POST['password'], $_POST['penca']);

			if($auth == 0){
				if(!$_SESSION) session_start();
				$_SESSION['user'] = $user;

				$redirector->gotoUrl('/pencas/partidos');
//				$redirector->gotoUrl('/admin/index');
			}
			else{
				$redirector->gotoUrl('/session/index?e='.$auth);
			}
		}
		else{
			$redirector->gotoUrl('/session/index?e=1');
		}
	}

	public function logoutAction()
	{
		if(!$_SESSION) session_start();
		if($_SESSION['user']) unset($_SESSION['user']);
		unset($_SESSION);
		session_destroy();

		$this->view->hide_menu = true;
		$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
		$redirector->gotoUrl('/session/index');
	}
	
	public function createNewPencaAction(){
		$this->view->hide_menu = true;
		$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
		$redirector->gotoUrl('/session/index');
	}

}

